package com.ht.medicaltreatment.activity

import android.content.Intent
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.TimeUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.adapter.SampleRecordAdapetr
import com.ht.medicaltreatment.bean.CentrifugeNormalRecordBean
import com.ht.medicaltreatment.bean.IceboxItemListBean
import com.ht.medicaltreatment.view.CentrifugeProjectDialog
import com.ht.medicaltreatment.view.RemarkDialog
import com.ht.medicaltreatment.viewmodel.CentrifugeRecordViewModel
import com.loper7.date_time_picker.DateTimeConfig
import com.loper7.date_time_picker.dialog.CardDatePickerDialog
import com.xgst.lib.utils.showToast
import kotlinx.android.synthetic.main.activity_centrifuge_record_layout.btnCha
import kotlinx.android.synthetic.main.activity_centrifuge_record_layout.cbNormal
import kotlinx.android.synthetic.main.activity_centrifuge_record_layout.cbOverTime
import kotlinx.android.synthetic.main.activity_centrifuge_record_layout.etFormName
import kotlinx.android.synthetic.main.activity_centrifuge_record_layout.etProjectName
import kotlinx.android.synthetic.main.activity_centrifuge_record_layout.groupNormal
import kotlinx.android.synthetic.main.activity_centrifuge_record_layout.imgProject
import kotlinx.android.synthetic.main.activity_centrifuge_record_layout.recyclerView
import kotlinx.android.synthetic.main.activity_centrifuge_record_layout.tvEndTime
import kotlinx.android.synthetic.main.activity_centrifuge_record_layout.tvStartTime

class CentrifugeRecordActivity : BaseActivity(), View.OnClickListener, OnItemClickListener,
    OnItemChildClickListener {

    override fun getLayoutId() = R.layout.activity_centrifuge_record_layout

    private val model by viewModels<CentrifugeRecordViewModel>()
    private val adapter = SampleRecordAdapetr()
    private val data:MutableList<IceboxItemListBean> = mutableListOf()
    private var projectId = ""
    private var centrifugeId = 0

    override fun isViewStup() = true

    override fun init() {
        setTitle("离心记录")
        centrifugeId = intent.getIntExtra("centrifugeId",-1)
        tvStartTime.setOnClickListener(this)
        tvEndTime.setOnClickListener(this)
        cbNormal.setOnClickListener(this)
        cbOverTime.setOnClickListener(this)
        btnCha.setOnClickListener(this)
        etProjectName.setOnClickListener(this)
        imgProject.setOnClickListener(this)

        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = adapter
        adapter.setOnItemClickListener(this)
        adapter.setOnItemChildClickListener(this)

        model.projects {
            data.clear()
            val list = mutableListOf<IceboxItemListBean>()
            it.forEach { bean->
                //list.add(IceboxItemListBean(bean.id,bean.name))
                list.add(IceboxItemListBean(bean.id,bean.protocolNumber))
            }
            data.addAll(list)
        }
    }

    override fun onClick(v: View?) {
        when(v){
            tvStartTime -> {
                CardDatePickerDialog.builder(this)
                    .setTitle("选择开始时间")
                    .showBackNow(false)
                    .showFocusDateInfo(false)
                    .setThemeColor(resources.getColor(R.color.blue))
                    .setBackGroundModel(CardDatePickerDialog.CUBE)
                    .setDisplayType(arrayListOf(
                        DateTimeConfig.YEAR,
                        DateTimeConfig.MONTH,
                        DateTimeConfig.DAY,
                        DateTimeConfig.HOUR,DateTimeConfig.MIN,DateTimeConfig.SECOND))
                    .setOnChoose {millisecond->
                        tvStartTime.text = TimeUtils.millis2String(millisecond,"yyyy-MM-dd HH:mm:ss")
                    }.build().show()
            }

            tvEndTime -> {
                CardDatePickerDialog.builder(this)
                    .setTitle("选择结束时间")
                    .showBackNow(false)
                    .showFocusDateInfo(false)
                    .setThemeColor(resources.getColor(R.color.blue))
                    .setBackGroundModel(CardDatePickerDialog.CUBE)
                    .setDisplayType(arrayListOf(
                        DateTimeConfig.YEAR,
                        DateTimeConfig.MONTH,
                        DateTimeConfig.DAY,
                        DateTimeConfig.HOUR,DateTimeConfig.MIN,DateTimeConfig.SECOND))
                    .setOnChoose {millisecond->
                        tvEndTime.text = TimeUtils.millis2String(millisecond,"yyyy-MM-dd HH:mm:ss")

                    }.build().show()
            }

            cbOverTime -> {
                cbNormal.isChecked = false
                cbOverTime.isChecked = true
                groupNormal.visibility = View.GONE
            }
            cbNormal -> {
                cbOverTime.isChecked = false
                cbNormal.isChecked = true
                groupNormal.visibility = View.VISIBLE
            }

            //一键查询
            btnCha -> {
                val formName = etFormName.text.toString()
                val tvStartTime = tvStartTime.text.toString()
                val tvEndTime = tvEndTime.text.toString()
                if(projectId.isEmpty()){
                    showToast("请选择项目")
                    return
                }
                showProgress()
                if(cbNormal.isChecked){
                    model.queryCentrifugeOrder(tvStartTime, "$centrifugeId",tvEndTime,formName,projectId,{
                        dismissProgress()
                        adapter.setNewInstance(it)
                    },{
                        dismissProgress()
                        showToast(it)
                    })
                }else{
                    model.overTimeList(projectId,{
                        dismissProgress()
                        val list = mutableListOf<CentrifugeNormalRecordBean>()
                        it?.forEach {
                            val bean = CentrifugeNormalRecordBean("NA",it.value,"NA",it.orderName)
                            list.add(bean)
                        }
                        adapter.setNewInstance(list)
                    },{
                        dismissProgress()
                        showToast(it)
                    })
                }
            }

            //选择项目
            etProjectName,
            imgProject -> {
                CentrifugeProjectDialog(this).show(data, { name, id ->
                    etProjectName.text = name
                    this.projectId = "$id"
                })
            }
        }
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = adapter.getItem(position) as CentrifugeNormalRecordBean
        val intent = Intent(this,CentrifugeRecordDetailActivity::class.java)
        intent.putExtra("orderIdOrValue",item.id)
        intent.putExtra("projectId",projectId)
        intent.putExtra("orderName",item.name)
        intent.putExtra("startTime",item.beginTime)
        intent.putExtra("remark",item.remark)
        startActivity(intent)
    }

    //添加备注
    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = adapter.getItem(position) as CentrifugeNormalRecordBean
        RemarkDialog(this).showDialog {
            showProgress()
            model.addRemark("${item.id}",it,{
                dismissProgress()
                item.remark = it
                adapter.notifyItemChanged(position)
            },{
                dismissProgress()
                showToast(it)
            })
        }
    }
}
